<?php

namespace app\controller;

use support\Request;
use app\service\Member as MemberService;

class Member
{

    // 成员
    public $member;

    public function __construct(MemberService $member)
    {
        $this->member = $member;
    }

    /**
     * 获取知识库权限信息
     * @param Request $request
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function select(Request $request)
    {
        $param = $request->all();
        $param['param']['filter']['user_id'] = $request->getCurrentUserId();
        $param['param']['filter']['tenant_id'] = $request->getTenantId();
        $res = $this->member->getMemberInfo($param['param']['filter']);
        return json(success_response('', $res));
    }

    /**
     * 编辑权限
     * @return \support\Response
     */
    public function update(Request $request)
    {
        $param = $request->all();
        $tenantId = $request->getTenantId();
        $data = $param['data'];
        $data['tenant_id'] = $tenantId;
        $this->member->update($data);
        return json(success_response('', $data));
    }


    /**
     * 删除权限
     * @param Request $request
     * @return \support\Response
     * @throws \Throwable
     */
    public function delete(Request $request)
    {
        $param = $request->all();
        $this->member->delete($param['param']['filter']);
        return json(success_response('', 1));
    }

    /**
     * 创建权限
     * @param Request $request
     * @return \support\Response
     * @throws \Throwable
     */
    public function create(Request $request)
    {
        $param = $request->all();
        $param['data']['tenant_id'] = $request->getTenantId();
        $param['data']['created_by'] = $request->getCurrentUserId();
        $this->member->create($param['data']);
        return json(success_response('', 1));
    }

}